﻿using POC.Controls.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace POC.Controls
{
    public class FloatingThumb : Thumb
    {
        public FloatingThumb()
        {
            this.DragDelta += FloatingThumb_DragDelta;
            this.Loaded += FloatingThumb_Loaded;
            this.PreviewMouseLeftButtonUp += FloatingThumb_PreviewMouseLeftButtonUp;
            this.HorizontalAlignment = HorizontalAlignment.Left;
            this.VerticalAlignment = VerticalAlignment.Top;
        }

        private void FloatingThumb_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!this.IsDraged)
            {
                //this.IsDown = !this.IsDown;
                ThumbClick?.Invoke();
            }
            this.IsDraged = false;
        }

        public delegate void ThumbClickEventHandler();
        public event ThumbClickEventHandler ThumbClick;
        //public bool IsDown
        //{
        //    get { return (bool)GetValue(IsDownProperty); }
        //    set { SetValue(IsDownProperty, value); }
        //}

        /// <summary>
        /// 麦克风状态
        /// </summary>
        public MicrophoneStateEnum PhoneState
        {
            get { return (MicrophoneStateEnum)GetValue(PhoneStateProperty); }
            set { SetValue(PhoneStateProperty, value); }
        }

        //// Using a DependencyProperty as the backing store for IsDown.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty IsDownProperty =
        //    DependencyProperty.Register("IsDown", typeof(bool), typeof(FloatingThumb), new PropertyMetadata(false));

        public static readonly DependencyProperty PhoneStateProperty =
            DependencyProperty.Register("PhoneState", typeof(MicrophoneStateEnum), typeof(FloatingThumb), new PropertyMetadata(MicrophoneStateEnum.Closed));


        private void FloatingThumb_Loaded(object sender, RoutedEventArgs e)
        {
            (this.Parent as FrameworkElement).SizeChanged += FloatingThumb_SizeChanged;
        }

        private void FloatingThumb_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.Margin = new Thickness(this.Margin.Left * e.NewSize.Width / e.PreviousSize.Width, this.Margin.Top * e.NewSize.Height / e.PreviousSize.Height, 0, 0);
        }

        public ImageSource FloatImageSource
        {
            get { return (ImageSource)GetValue(FloatImageSourceProperty); }
            set { SetValue(FloatImageSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FloatImageSourceProperty =
            DependencyProperty.Register("FloatImageSource", typeof(ImageSource), typeof(FloatingThumb));

        public bool IsDraged { get; set; }
        private void FloatingThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            this.IsDraged = true;
            var parentWidth = (this.Parent as Grid).ActualWidth;
            var parentHeight = (this.Parent as Grid).ActualHeight;

            double left = this.Margin.Left + e.HorizontalChange > parentWidth - this.ActualWidth ? parentWidth - this.ActualWidth :
                (this.Margin.Left + e.HorizontalChange < 0 ? 0 : this.Margin.Left + e.HorizontalChange);
            double top = this.Margin.Top + e.VerticalChange > parentHeight - this.ActualHeight ? parentHeight - this.ActualHeight :
                (this.Margin.Top + e.VerticalChange < 0 ? 0 : this.Margin.Top + e.VerticalChange);
            this.Margin = new Thickness(left, top, 0, 0);
        }
    }
}